package com.mvp.shq.washer.ui.userCenter;

import com.mvp.shq.washer.entity.result.BaseResult;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.utils.DateUtils;
import com.mvp.shq.washer.utils.SpUtil;

import java.util.Date;

import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/8/24.
 */

public class UserCenterPresenter extends UserCenterConstract.Presenter {
    String token;
    String userId;
    @Override
    public void onStart() {
        token = SpUtil.getToken();
        userId = SpUtil.getUser().getId();
    }

    @Override
    void updateBirthday(Date birthday) {
        model.updateBirthday(token,userId, DateUtils.getDate(birthday))
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (t.isError()){
                            view.uBirthdayF(t.getMsg());
                        }else {
                            view.uBirthDayS();
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        view.uBirthdayF("修改生日出错啦");
                        e.printStackTrace();
                    }
                });
    }

    @Override
    void updateGender(String gender) {
        model.updateGender(token,userId,gender)
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (t.isError()){
                            view.uGenderF(t.getMsg());
                        }else {
                            view.uGenderS();
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        view.uGenderF("修改性别出错啦");
                    }
                });
    }

    @Override
    void updateNickname(String nickname) {
        model.updateNickname(token,userId,nickname)
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (t.isError()){
                            view.uNickNameF(t.getMsg());
                        }else {
                            view.uNicknameS();
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        view.uNickNameF("修改昵称出错啦");
                    }
                });
    }

    @Override
    void updateFace(RequestBody face) {
        model.updateFace(token,userId,face)
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (t.isError()){
                            view.uFaceF(t.getMsg());
                        }else {
                            view.uFaceS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        view.uFaceF("修改头像出错啦");
                    }
                });
    }
}
